package com.zy.hospitalward_management_system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zy.hospitalward_management_system.pojo.*;
import com.zy.hospitalward_management_system.service.impl.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 控制页面跳转Controller
 */
@Controller
public class RouterController {

    @Resource
    DepartmentServiceImpl departmentService;

    @Resource
    PatientServiceImpl patientService;

    @Resource
    InpatientwardServiceImpl inpatientwardService;

    @Resource
    DoctorServiceImpl doctorService;

    @Resource
    DrugServiceImpl drugService;

    @Resource
    LogServiceImpl logService;

    @Resource
    BedServiceImpl bedService;

    @Resource
    StaffServiceImpl staffService;

    /**
     * 跳转到首页
     * @return
     */
    @RequestMapping("/toDashBoard")
    public String toDashBoard(Model model){
        List<Log> recentLogs = logService.getBaseMapper().queryRecentLogs();
        model.addAttribute("recentLogs",recentLogs);

        //空闲床位统计
        Integer bed_empty = bedService.getBaseMapper().selectCount(new QueryWrapper<Bed>().eq("bed_state", 0));
        //已入住床位统计
        Integer bed_used = bedService.getBaseMapper().selectCount(new QueryWrapper<Bed>().eq("bed_state", 1));

        List<Integer> bed_count = new ArrayList<>();
        bed_count.add(bed_empty);
        bed_count.add(bed_used);
        model.addAttribute("bed_count",bed_count);
        return "user/dashboard";
    }


    /**
     * 跳转到病人登记页面
     * @return
     */
    @RequestMapping("/toPatientCheckin")
    public String toPatientCheckin(Model model){
        List<Department> departments = departmentService.getBaseMapper().selectList(null);
        List<Drug> drugs = drugService.getBaseMapper().selectList(null);
        model.addAttribute("drugs",drugs);
        model.addAttribute("departments",departments);
        return "user/patient_checkin";
    }

    /**
     * 跳转到病人管理页面
     * @return
     */
    @RequestMapping("/toPatientList")
    public String toPatientList(Model model){
        List<Patient> patients = patientService.getBaseMapper().selectList(null);

        model.addAttribute("patients",patients);
        return "user/list/patient_list";
    }

    /**
     * 跳转到病房管理页面
     * @return
     */
    @RequestMapping("/toInpatientwardList")
    public String toInpatientwardList(Model model){
        List<Map<String, String>> infoMaps = inpatientwardService.getBaseMapper().inpatientWardsManagePageInfo();
        List<Map<String, String>> departmentKinds = departmentService.getBaseMapper().selectDepartmentKinds();
        model.addAttribute("departmentKinds",departmentKinds);
        model.addAttribute("infoMaps",infoMaps);
        return "user/list/inpatientward_list";
    }



    /**
     * 跳转到医生管理页面
     * @return
     */
    @RequestMapping("/toDoctorList")
    public String toDoctorList(Model model){
        List<Map<String, String>> maps = doctorService.getBaseMapper().queryAllDoctorsInfo();
        List<Department> departments = departmentService.getBaseMapper().selectList(null);
        model.addAttribute("maps",maps);
        model.addAttribute("departments",departments);
        return "user/list/doctor_list";
    }

    /**
     * 跳转到科室管理页面
     * @return
     */
    @RequestMapping("/toDepartmentList")
    public String toDepartmentList(Model model){
        List<Department> departments = departmentService.getBaseMapper().selectList(null);
        model.addAttribute("departments",departments);
        return "user/list/department_list";
    }


    /**
     * 跳转到药品管理页面
     * @return
     */
    @RequestMapping("/toDrugList")
    public String toDrugList(Model model){
        List<Drug> drugs = drugService.getBaseMapper().selectList(null);
        model.addAttribute("drugs",drugs);
        return "user/list/drug_list";
    }


    /**
     * 跳转到出入院记录页面
     * @return
     */
    @RequestMapping("/toLogList")
    public String toLogList(Model model){
        List<Log> logs = logService.getBaseMapper().selectList(new QueryWrapper<Log>().orderByDesc("log_date"));
        model.addAttribute("logs",logs);
        return "user/list/log_list";
    }


    /**
     * 跳转到出院办理页面
     */
    @RequestMapping("/toPatientCheckout")
    public String toPatientCheckout(Model model){
        List<Patient> patients = patientService.getBaseMapper().selectList(new QueryWrapper<Patient>().isNotNull("bed_id"));
        model.addAttribute("patients",patients);
        List<Department> departments = departmentService.getBaseMapper().selectList(null);
        model.addAttribute("departments",departments);
        List<Doctor> doctors = doctorService.getBaseMapper().selectList(null);
        model.addAttribute("doctors",doctors);
        return "user/patient_checkout";
    }

    /**
     * 跳转到员工个人信息页面
     * @return
     */
    @RequestMapping("/toStaffInfo")
    public String toStaffInfo(Model model,HttpSession session){
        Staff staffInfo = (Staff) session.getAttribute("loginUser");
        Staff staff = staffService.getBaseMapper().selectById(staffInfo.getStaffId());
        model.addAttribute("staff",staff);
        return "user/info/staff_info";
    }

    @RequestMapping("/toIndex")
    public String toIndex(){
        return "index";
    }


}
